package com.yonyou.pmclouds.basecom.util;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class EncryptUtils {
    public static String sign(String s,String type) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance(type);
            byte[] input = s.getBytes("UTF-8");
            byte[] buff = md.digest(input);
            String sign = new BigInteger(1, buff).toString(16);
            for (int i = sign.length(); i < 32; i++) {
                // 如果出现高位是0的情况，需要补零
                sign = '0' + sign;
            }
            return sign;
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            log.error("签名失败，原因", e);
        }
        return "";
    }
    public static String signWithSHA(String s) {
        return sign(s,"SHA");
    }
    public static String signWithMd5(String s) {
        return sign(s,"MD5");
    }

}
